package cn.jekun.shop.common.service;

import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.LsLog;
import cn.jekun.component.core.service.gen.ErpShopService;
import cn.jekun.component.core.service.gen.LsLogService;

@Service(DomainService.BEAN_NAME)
public class DomainService extends ShopBaseService{
	public static final String BEAN_NAME = "cn.jekun.shop.common.service.DomainService";
	@Resource(name = ErpShopService.BEAN_NAME)
	ErpShopService erpShopService;
	@Resource(name = LsLogService.BEAN_NAME)
	LsLogService lsLogService;
	/**
	 * 查询商家域名
	 * @param shopId
	 * @return
	 */
	public JSONObject queryDomainByShopId(@ErpShop.AnnoShopId String shopId) {
		JSONObject jsonObject = new JSONObject();
		this.setDomainNums(shopId);
		ErpShop shop = this.erpShopService.one(shopId);
		String secondDomain = "";
		String firstDomain = "";
		String nums = ""+(3-this.setDomainNums(shopId));
		if(shop != null && !("".equals(shop.getUrl()))){
			secondDomain = "http://"+shop.getUrl()+".storeme.cn";
		}
		firstDomain = "http://"+shopId+".storeme.cn";
		jsonObject.put("firstDomain", firstDomain);
		jsonObject.put("secondDomain", secondDomain);
		jsonObject.put("nums", nums);
		return executeRerutn(jsonObject);
	}
	/**
	 * 修改商家域名
	 * @param shopId
	 * @param secondDomain
	 * @return
	 */
	public JSONObject updateDomainByShopId(@ErpShop.AnnoShopId String shopId, @ErpShop.AnnoUrl String secondDomain) {
		JSONObject jsonObject = new JSONObject();
		//查询商家二级域名设置的次数
		int nums = this.setDomainNums(shopId);
		if(nums<3){
			//判断当前域名是否可用
			boolean falg = this.isReg(shopId, secondDomain);
			if(falg){
				jsonObject.element(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
				jsonObject.element(ErrorMsg.KEY_MESSAGE, "此域名已存在");
				return executeRerutnFail(jsonObject);
			}else{
				ErpShop shop = new ErpShop();
				shop.setShopId(shopId);
				shop.setUrl(secondDomain);
				int totals = this.erpShopService.update(shop);
				if(totals != 1){
					return executeRerutnFail(jsonObject);
				}
				return executeRerutn(jsonObject);
			}
		}else{
			jsonObject.put(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.put(ErrorMsg.KEY_MESSAGE,"您的二级域名最多只能设置3次");
			return executeRerutnFail(jsonObject);
			
		}
		
	}
	
	/**
	 * 检查二级域名是否被注册过
	 * @param secondDomain
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean isReg(@ErpShop.AnnoShopId String shopId,@ErpShop.AnnoUrl String secondDomain){
		Map formMap = new HashMap();
		formMap.put(ErpShop.COL_URL, secondDomain);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap,"a."+ErpShop.COL_URL,Types.VARCHAR,SqlUtis.EQ));
//		ErpShop shop = this.erpShopService.one(shopId);
//		if( shop != null && secondDomain.equals(shop.getUrl())){
//			return true;
//		}else{
			return this.erpShopService.exists(formMap);
//		}
	}
	/**
	 * 检查二级域名是否被注册过
	 * @param secondDomain
	 * @return
	 */
	public JSONObject checkIsReg(@ErpShop.AnnoShopId String shopId, @ErpShop.AnnoUrl String secondDomain) {
		JSONObject jsonObject = new JSONObject();
		//判断当前域名是否可用
		boolean falg = this.isReg(shopId, secondDomain);
		if(falg){
			jsonObject.element(ErrorMsg.KEY_CODE, ErrorMsg.CALL_FAIL);
			jsonObject.element(ErrorMsg.KEY_MESSAGE, "此域名已存在");
			return executeRerutnFail(jsonObject);
		}else{
			jsonObject.element(ErrorMsg.KEY_CODE, ErrorMsg.CALL_OK);
			jsonObject.element(ErrorMsg.KEY_MESSAGE, "此域名可以使用");
			return executeRerutn(jsonObject);
		}
		
	}
	/**
	 * 查询商家域名设置的次数
	 * @param shopId
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public int setDomainNums(@ErpShop.AnnoShopId String shopId){
		Map formMap = new HashMap();
		formMap.put(LsLog.COL_MEMO, "商家编码:"+shopId+"二级域名设置成功");
		SqlUtis.prepareSql(formMap, SqlUtis.getSQLRequired(formMap,"a."+LsLog.COL_MEMO,Types.VARCHAR,SqlUtis.EQ));
		return this.lsLogService.count(formMap);
	}
}
